import java.util.HashMap;
import java.util.Map;

/**
 * 传送带上的包裹必须在 D 天内从一个港口运送到另一个港口。
 * <p>
 * 传送带上的第 i 个包裹的重量为 weights[i]。每一天，我们都会按给出重量的顺序往传送带上装载包裹。我们装载的重量不会超过船的最大运载重量。
 * <p>
 * 返回能在 D 天内将传送带上的所有包裹送达的船的最低运载能力。
 * 示例 1：
 * <p>
 * 输入：weights = [1,2,3,4,5,6,7,8,9,10], D = 5
 * 输出：15
 * 解释：
 * 船舶最低载重 15 就能够在 5 天内送达所有包裹，如下所示：
 * 第 1 天：1, 2, 3, 4, 5
 * 第 2 天：6, 7
 * 第 3 天：8
 * 第 4 天：9
 * 第 5 天：10
 * <p>
 * 请注意，货物必须按照给定的顺序装运，因此使用载重能力为 14 的船舶并将包装分成 (2, 3, 4, 5), (1, 6, 7), (8), (9), (10) 是不允许的。
 * <p>
 */
class Solution {

    public static void main(String[] args) {
        System.out.println(shipWithinDays(
                new int[]{180, 373, 75, 82, 497, 23, 303, 299, 53, 426, 152, 314, 206, 433, 283, 370, 179, 254, 265, 431, 453, 17, 189, 224}, 12));
    }

    private static Map<String, Integer> map;
    private static int count = 0;

    /**
     * 二分法
     *
     * @param weights
     * @param d
     * @return
     */
    public static int shipWithinDays(int[] weights, int d) {
        map = new HashMap<>(weights.length);
        return shipWithinDays(weights, 0, weights.length - 1, d);
    }

    public static int shipWithinDays(int[] weights, int start, int end, int d) {
        String key = start + "-" + end + "-" + d;
        if (map.containsKey(key)) {
            return map.get(key);
        }
        int tmp = 0;
        if (end - start + 1 == d) {
            for (int i = start; i <= end; i++) {
                tmp = Math.max(tmp, weights[i]);
            }
        } else if (d == 1) {
            // 求和
            for (int i = start; i <= end; i++) {
                tmp += weights[i];
            }
        } else if (d == 2) {
            // 双指针找中间值
            int middle = (start + end) / 2;
            int leftVal = 0, rightVal = 0;
            for (int i = start; i <= middle; i++) {
                leftVal += weights[i];
            }
            for (int i = end; i > middle; i--) {
                rightVal += weights[i];
            }
            tmp = Math.max(leftVal, rightVal);
            if (leftVal < rightVal) {
                // middle右移
                while (leftVal < rightVal && middle < end) {
                    middle++;
                    leftVal += weights[middle];
                    rightVal -= weights[middle];
                    tmp = Math.min(tmp, Math.max(leftVal, rightVal));
                }
            } else if (leftVal > rightVal) {
                // middle左移
                while (leftVal > rightVal && middle > start) {
                    leftVal -= weights[middle];
                    rightVal += weights[middle];
                    tmp = Math.min(tmp, Math.max(leftVal, rightVal));
                    middle--;
                }
            }
        } else {
            // 二分
            int middle = (start + end) / 2;
            int leftDay = d - d / 2;
            int rightDay = d / 2;
            int leftVal = shipWithinDays(weights, start, middle, leftDay);
            int rightVal = shipWithinDays(weights, middle + 1, end, rightDay);
            tmp = Math.max(leftVal, rightVal);
            if (leftVal < rightVal) {
                // middle右移
                while (leftVal < rightVal && middle < end) {
                    middle++;
                    leftVal = shipWithinDays(weights, start, middle, leftDay);
                    rightVal = shipWithinDays(weights, middle + 1, end, rightDay);
                    tmp = Math.min(tmp, Math.max(leftVal, rightVal));
                }
            } else if (leftVal > rightVal) {
                // middle左移
                while (leftVal > rightVal && middle > start) {
                    middle--;
                    leftVal = shipWithinDays(weights, start, middle, leftDay);
                    rightVal = shipWithinDays(weights, middle + 1, end, rightDay);
                    tmp = Math.min(tmp, Math.max(leftVal, rightVal));
                }
            }
        }
        //System.out.println("start " + start + ", end " + end + ", d " + d + ", tmp " + tmp);
        map.put(key, tmp);
        System.out.println(count++);
        return tmp;
    }


}